import 'package:flutter/material.dart';

/**
 * StatefulWidget 动态列表
 */
class StatefulWidgetListDemo extends StatefulWidget {
  const StatefulWidgetListDemo({super.key});

  @override
  State<StatefulWidgetListDemo> createState() => _StatefulWidgetListDemoState();
}

class _StatefulWidgetListDemoState extends State<StatefulWidgetListDemo> {
  //----------final定义常量只能赋值一次
  //----------final定义list,可以给list多次增加数据(不是赋值)
  //私有，先声明在赋值只能赋值一次，运行时常量
  final List<String> _list = [];
  int data = 0;

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: ListView(
        ///循环添加数据
        children: _list
            .map((value) => ListTile(
                  title: Text(value + '=$data'),
                ))
            .toList(),
      ),

      ///动态添加数据,更新数据重新调用build方法
      floatingActionButton: FloatingActionButton(
        child: Icon(Icons.add),
        onPressed: () {
          setState(() {
            _list.add("我是一个新增的列表数据");
            data++;
          });
        },
      ),
    );
  }
}
